import mx.utils.Delegate;

import org.molamil.acto.core.EventConstants;
import org.molamil.acto.core.Logger;
import org.molamil.acto.pages.PagesConf;
import org.molamil.acto.pages.controller.XmlPagesController;
import org.molamil.acto.pages.factory.BasicPageFactory;
import org.molamil.acto.pages.factory.XmlPagesFactory;
import org.molamil.acto.pages.preload.BasicPagePreloader;
import org.molamil.acto.pages.preload.PagePinger;
import org.molamil.acto.pages.preload.PagePreloader;
import org.molamil.commons.EventUtil;

/**
 * XXX: Comment.
 * 
 * @author Jorge Hernandez
 */
class org.molamil.acto.pages.controller.PreloadController extends XmlPagesController implements PagePreloader, PagePinger {
	
	
	// Properties
	
	private static var log:Logger = Logger.getLogger("PreloadController");
	
	private var preloader:BasicPagePreloader;
	
	private var loadConfDelegate:Function;
	private var pingDelegate:Function;
	private var pageLoadStartDelegate:Function;	private var pageLoadInitDelegate:Function;
	
	
	// Constructor
	
	public function PreloadController() {
		
		log.debug("Constructor");
		
		loadConfDelegate = Delegate.create(this, loadConfListener);
		pingDelegate = Delegate.create(this, pingListener);		pageLoadStartDelegate = Delegate.create(this, pageLoadStartListener);		pageLoadInitDelegate = Delegate.create(this, pageLoadInitListener);
		
	}
	
	
	// Functions
	
	public function loadConf(source:Object):Void {
		log.info("loadConf()");
		super.loadConf(source);
		XmlPagesFactory(factory).addEventListener(EventConstants.PAGES_CONF_LOAD, loadConfDelegate);
	}
	
	public function ping():Void {
		log.info("ping()");
		preloader.ping();
	}
	
	function stopPing():Void {
		log.info("stopPing()");
		preloader.stopPing();
	}
	
	public function preload():Void {
		log.info("preload()");
		preloader.preload();
	}
	
	public function stopPreload():Void {
		log.info("stopPreload()");
		preloader.stopPreload();
	}
	
	public function restartPreload():Void {
		log.info("restartPreload()");
		preloader.restartPreload();
	}
	
	
	// Listeners
	
	private function loadConfListener(eventObject:Object):Void {
		
		log.debug("loadConfListener()");
		
		preloader = new BasicPagePreloader(BasicPageFactory(factory).getPages());
		
		EventUtil.propagate(preloader, this, EventConstants.PAGE_PING_START);
		EventUtil.propagate(preloader, this, EventConstants.PAGE_PING_COMPLETE);
		EventUtil.propagate(preloader, this, EventConstants.PAGE_PING_ERROR);
		EventUtil.propagate(preloader, this, EventConstants.PAGES_PING_START);
		EventUtil.propagate(preloader, this, EventConstants.PAGES_PING_COMPLETE);
		EventUtil.propagate(preloader, this, EventConstants.PAGE_PRELOAD_START);
		EventUtil.propagate(preloader, this, EventConstants.PAGE_PRELOAD_PROGRESS);
		EventUtil.propagate(preloader, this, EventConstants.PAGE_PRELOAD_INIT);
		EventUtil.propagate(preloader, this, EventConstants.PAGE_PRELOAD_ERROR);
		EventUtil.propagate(preloader, this, EventConstants.PAGES_PRELOAD_START);
		EventUtil.propagate(preloader, this, EventConstants.PAGES_PRELOAD_PROGRESS);
		EventUtil.propagate(preloader, this, EventConstants.PAGES_PRELOAD_INIT);
		EventUtil.propagate(preloader, this, EventConstants.PAGES_PRELOAD_STOP);
		EventUtil.propagate(preloader, this, EventConstants.PAGES_PRELOAD_RESTART);
		
		preloader.addEventListener(EventConstants.PAGES_PING_COMPLETE, pingDelegate);
		
		if (PagesConf.isPagePausePreload) {			addEventListener(EventConstants.PAGE_LOAD_START, pageLoadStartDelegate);			addEventListener(EventConstants.PAGE_LOAD_INIT, pageLoadInitDelegate);			addEventListener(EventConstants.PAGE_LOAD_ERROR, pageLoadInitDelegate);
		}
		
		log.debug("PagesConf.isPing = " + PagesConf.isPing);		log.debug("PagesConf.isPreload = " + PagesConf.isPreload);
		
		if (PagesConf.isPing) {
			ping();
		} else if (PagesConf.isPreload) {
			preload();
		} 
		
	}
	
	private function pingListener(eventObject:Object):Void {
		if (PagesConf.isPreload)
			preload();
	}
	
	private function pageLoadStartListener(eventObject:Object):Void {
		if (preloader.isPinging)
			stopPing();
		if (preloader.isPreloading)
			stopPreload();
	}
	
	private function pageLoadInitListener(eventObject:Object):Void {
		if (preloader.isPinging)
			ping();
		if (preloader.isPreloading)
			restartPreload();
	}
	

}